<?php
defined('_JEXEC') or die();

class WrdControllerCpoint extends JControllerLegacy 
{
	protected $option;
	protected $controller; 
		
	function __construct( $default = array()){
   		$this->option = 'com_wrd';
   		$this->controller = 'cpoint';
		parent::__construct( $default );
   	}

   	function display()
	{
		switch($this->getTask()){
			case 'add':{
				JRequest::setVar( 'hidemainmenu', 1 );
				JRequest::setVar( 'layout', 'form'  );
				JRequest::setVar( 'view'  , 'cpoint');
			} 
			break;
            default:
				JRequest::setVar( 'view'  , 'cpoint');
	           	break;
		}
		parent::display();
	}
        
      //save profile info
    function save(){
    	
	   $post=JRequest::get('post');
       $post['created_date'] = date("Y-m-d H:i:s");
       $params = JComponentHelper::getParams('com_wrd');
       $model = $this->getModel('cpoint');
	   
       
       if(!isset($post['mon_r'])) $post['mon_r'] = 0;
       if(!isset($post['tue_r'])) $post['tue_r'] = 0;
       
       if(!isset($post['wed_r'])) $post['wed_r'] = 0;
       if(!isset($post['thu_r'])) $post['thu_r'] = 0;
       if(!isset($post['fri_r'])) $post['fri_r'] = 0;
       if(!isset($post['sat_r'])) $post['sat_r'] = 0;
       if(!isset($post['sun_r'])) $post['sun_r'] = 0;
       
       if(!isset($post['f'])) $post['f'] = 0;
       if(!isset($post['b'])) $post['b'] = 0;
       if(!isset($post['o'])) $post['o'] = 0;
       if(!isset($post['t'])) $post['t'] = 0;
       if(!isset($post['m'])) $post['m'] = 0;
       if(!isset($post['a'])) $post['a'] = 0;
       if(!isset($post['e'])) $post['e'] = 0;
       
       //get date current  
       $outlet = $this->getModel("outlets");
	   
       $id_category = $model->store($post,'cpoint',$outlet->getList());
       if($id_category != 0){
       		WrdHelper::sendEmailPointNotice($post);
            $this->setRedirect('index.php?option=com_wrd&controller=cpoint','Saved');
            return true;
       }
       else{
            $this->setRedirect('index.php?option=com_wrd&controller=cpoint','Error Saved');
            return false;
       }
    }    
   
    function remove()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);

		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Select an item to delete' ) );
		}

		$model = $this->getModel('cpoint');
		if(!$model->delete($cid)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}
		$this->setRedirect( 'index.php?option=com_wrd&controller=cpoint' );
	}
   
    function publish()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);

		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( '<p style="color:red">Please select an item to publish</p>' ) );
		}

		$model = $this->getModel('cpoint');
		if(!$model->publish($cid, 1)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}
		$this->setRedirect( 'index.php?option=com_wrd&controller=cpoint' );
	}

	function unpublish(){
		
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);

		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( '<p style="color:red">Please select an item to publish</p>'  ) );
		}

		$model = $this->getModel('cpoint');
		
		if(!$model->publish($cid, 0)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}
		
		$this->setRedirect( 'index.php?option=com_wrd&controller=cpoint' );
	}
}
?>